package ph.h.Helper;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import ph.h.model.Sentence;
import ph.h.model.Time;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.R.string;
import android.content.res.Resources.Theme;
import android.os.Environment;

public class XMLHelper {
	/**
	 * Zero-base
	 * */
	public static Sentence getTimeMapping(int order, String path,
			String fileName) throws ParserConfigurationException, SAXException,
			IOException {
		if (fileName == "" || fileName == null) {
			return null;
		} else {
			path += "/" + fileName + ".xml";
		}
		Sentence sentence = new Sentence();
		// read XML
		Document dom;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		File file = new File(Environment.getExternalStorageDirectory(), path);
		dom = db.parse(file);
		Element docEle = dom.getDocumentElement();
		NodeList senNodes = docEle.getElementsByTagName("sentence");
		if (senNodes != null && senNodes.getLength() > 0) {
			Element sen = (Element) senNodes.item(order);
			// get start
			NodeList startNode = sen.getElementsByTagName("start");
			int sh = Integer.parseInt(startNode.item(0).getAttributes()
					.getNamedItem("hh").getTextContent());
			int sm = Integer.parseInt(startNode.item(0).getAttributes()
					.getNamedItem("mm").getTextContent());
			int ss = Integer.parseInt(startNode.item(0).getAttributes()
					.getNamedItem("ss").getTextContent());
			int sms = Integer.parseInt(startNode.item(0).getAttributes()
					.getNamedItem("mss").getTextContent());
			Time tStart = new Time(sh, sm, ss, sms);
			sentence.setSt(tStart);
			// get end
			NodeList endNode = sen.getElementsByTagName("end");
			int eh = Integer.parseInt(endNode.item(0).getAttributes()
					.getNamedItem("hh").getTextContent());
			int em = Integer.parseInt(endNode.item(0).getAttributes()
					.getNamedItem("mm").getTextContent());
			int es = Integer.parseInt(endNode.item(0).getAttributes()
					.getNamedItem("ss").getTextContent());
			int ems = Integer.parseInt(endNode.item(0).getAttributes()
					.getNamedItem("mss").getTextContent());
			Time sEnd = new Time(eh, em, es, ems);
			sentence.setEn(sEnd);
		}
		return sentence;
	}

	public static int getPosition(String path, String fileName) {
		int position = 0;
		if (fileName == "" || fileName == null) {
			return 0;
		} else {
			path += "/" + fileName + ".xml";
		}
		// read XML
		Document dom;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			File file = new File(Environment.getExternalStorageDirectory(),
					path);
			dom = db.parse(file);
			Element docEle = dom.getDocumentElement();
			NodeList positionNode = docEle.getElementsByTagName("position");
			if (positionNode != null && positionNode.getLength() > 0) {
				position = Integer.parseInt(positionNode.item(0)
						.getTextContent());
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return position;
	}

	public static void updatePosition(String path, String fileName, int position)
			throws ParserConfigurationException, SAXException, IOException,
			TransformerException {
		String filepath = path + "/" + fileName + ".xml";
		File f = new File(Environment.getExternalStorageDirectory(), filepath);
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(f);

		// Get the root element
		Node mapping = doc.getFirstChild();


		// Get the staff element by tag name directly
		Node pos = doc.getElementsByTagName("position").item(0);
		pos.setTextContent(String.valueOf(position));

		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(f);
		transformer.transform(source, result);
	}
}
